<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>商品入库</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		.table td {
			vertical-align: middle;
		}
		.footer {
			position:fixed; bottom:0; background:#ABABAB; width:100%; height:35px; line-height:32px; z-index:9999; opacity:1;
			filter:alpha(opacity=60); _bottom:auto; _width:100%; _position:absolute;text-align: right;
			_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-
			(parseInt(this.currentStyle.marginTop, 10)||0)-(parseInt(this.currentStyle.marginBottom, 10)||0)));
	    }
	</style>
	<script type="text/javascript">
		$(document).ready(function() {
			var nodeLen = $(".jbox-button-panel", top.document).length;
			$(".jbox-button-panel", top.document).eq(nodeLen-1).remove();
			if('${type}'!=''){
				if(${type==1}){  //提交/关闭
					parent.mainFrame.location.reload();
					top.$.jBox.close();
				}else{  //提交/继续入库
					parent.mainFrame.location.reload();
					document.location="${ctx}/ivm/inventory/showIntoInventory?&productId=${productInventoryDetail.product.id}&purchaseId=${purchaseId}";
				}
			}
			
			$("#code").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				rules: {
					weight:{  positiveinteger:true}
				},   
				message:{
					weight:{  positiveinteger:"请输入正整数"}
				},   
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
		function checkProductCode(){
			var obj = $("#code");
			if($(obj).val()!=""){
				//异步保存活动
				$.ajax({
					type: 'post',
					url: '${ctx}/ivm/inventory/checkProductCode',
					data: 'merchantId=${merchant.id}&code='+$(obj).val(),
					dataType: 'json',
					success: function(data){
						if(data.status==200){
							var product = data.result;
							$(".unitName").each(function(){
								$(this).text(product.specUnit.name);
							});
						}else{
							showJBoxMess(data.message,"error");
							$("#code").select();
							$(".unitName").each(function(){
								$(this).text("");
							});
						}
					},
					failure: function(){
						showJBoxMess("操作失败","error");
					}				
				});
			}
		}
		
		function showLossWeight(obj){
			updateNum(obj, 0);
			$("#winWeight").val($("#weight").val());
			var lossWeight = accSub($("#weight").val(), $("#winWeight").val());
			$("#lossWeight").val(lossWeight);
		}
	</script>
</head>
<body>
<form:form id="inputForm" modelAttribute="productInventoryDetail" action="${ctx}/ivm/inventory/createIntoInventory" method="post" class="form-horizontal">
	<tags:message content="${message}"/>
	<input type="hidden" id="type" name="type" value="${type }"/>
	<form:hidden path="purchase.id"/>
	<form:hidden path="id"/>
	<form:hidden path="merchant.id"/>
	<div style="margin:10px;">
		<c:if test="${productInventoryDetail.product!=null && productInventoryDetail.product.code!=0}">
		<div class="control-group">
			<label class="control-label"><span class="color-red font-size20">*</span>商品名称:</label>
			<div class="controls">
				<form:hidden id="code" path="product.code"/>
				${productInventoryDetail.product.name }
			</div>
		</div>
		</c:if>
		<c:if test="${productInventoryDetail.product==null || productInventoryDetail.product.code==0}">
		<div class="control-group">
			<label class="control-label"><span class="color-red font-size20">*</span>商品:</label>
			<div class="controls">
				<form:input id="code" path="product.code" htmlEscape="false" maxlength="200" class="required" onblur="checkProductCode()" placeholder="请输入商品编号或名称"/>
			</div>
		</div>
		</c:if>
		<div class="control-group ${productInventoryDetail.purchase!=null && productInventoryDetail.purchase.id!=''?'hidden':'' }">
			<label class="control-label"><span class="color-red font-size20">*</span>采购员:</label>
			<div class="controls">
				<form:select id="member" path="member.id" class="required">
					<form:option value="" label="请选择"/>
					<form:options items="${fns:getDeliveryMemberList(2)}" itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label"><span class="color-red font-size20">*</span>采购量:</label>
			<div class="controls">
				<form:input path="weight" htmlEscape="false" maxlength="200" class="required digits number input-small" min="1" onblur="showLossWeight(this)"/>
				<label class="unitName">${productInventoryDetail.product.specUnit.name }</label>
			</div>
		</div>
		<div class="control-group hidden">
			<label class="control-label"><span class="color-red font-size20">*</span>入库量:</label>
			<div class="controls">
				<form:input path="winWeight" htmlEscape="false" maxlength="200" ltEq="#weight" class="required digits number input-small" min="1" onblur="showLossWeight(this)"/>
				<label class="unitName">${productInventoryDetail.product.productUnit.name }</label>
			</div>
		</div>
		<div class="control-group hidden">
			<label class="control-label">损耗量:</label>
			<div class="controls">
				<form:input path="lossWeight" htmlEscape="false" maxlength="200" class="digits number input-small" min="0" readonly="true"/>
				<label class="unitName">${productInventoryDetail.product.productUnit.name }</label>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label"><span class="color-red font-size20">*</span>采购金额:</label>
			<div class="controls">
				<form:input path="amount" htmlEscape="false" maxlength="200" class="required number decimal input-small" min="0.1"/>元
			</div>
		</div>
		<div class="control-group hidden">
			<label class="control-label"><span class="color-red font-size20">*</span>采购供应商:</label>
			<div class="controls">
				<form:select id="supplier" path="supplier.id">
					<form:option value="" label="请选择"/>
					<form:options items="${fns:getSupplierList()}" itemLabel="name" itemValue="id" htmlEscape="false"/>
				</form:select>
			</div>
		</div>
		<div class="control-group hidden">
			<label class="control-label">备注:</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="3" maxlength="200"/>
			</div>
		</div>
	</div>
</form:form>
<!--endprint-->
<div class="footer" id="printDiv">
	<input value="提交/继续入库" type="button" class="btn btn-primary" style="margin-right: 20px;" onclick="$('#type').val(2);$('#inputForm').submit();"/>
	<input value="提交/关闭" type="button" class="btn btn-primary" style="margin-right: 20px;" onclick="$('#type').val(1);$('#inputForm').submit();"/>
</div>
</body>
</html>